package com.zhuss.order.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.SearchOrderBo;
import com.zhuss.bean.bo.order.order.SearchOrderItemBo;
import com.zhuss.bean.bo.order.order.app.ListTicketBo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.ListOrderItemVo;
import com.zhuss.bean.vo.order.order.app.ListTicketVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.entity.OrderItem;
import com.zhuss.util.DateUtil;


/**
 * @description: 订单明细读
 * @author: zhushanshuo
 * @create: Tue Jan 12 13:51:36 CST 2021
 **/
@Component("orderItemRead")
public class OrderItemRead {

    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private OrderRead orderRead;

    @Autowired
    private UserDao userDao;

    
    
    public ListTicketVo latestTicket(Long userId) {
    	ListTicketVo result = orderItemDao.latestTicket(userId, new Date(System.currentTimeMillis() - 1000 * 3600 * 24 * 10), OrderStatus.支付成功.key());
		return result;
	}
    
    /**
     * 订单明细
     *
     * @return
     */
    public Object listOrderItem(List<Long> orderItemIdList) {
        List<OrderItem> result = orderItemDao.listById(orderItemIdList);
        return result;
    }


    /**
     * 根据名字查询订单明细
     *
     * @param
     * @return
     */
    public List<OrderItem> list(String name) {
        List<OrderItem> result = orderItemDao.listByName(name);
        return result;
    }

    public List<OrderItem> listNotId(String name,Long orderItemId) {
        List<OrderItem> result = new ArrayList<OrderItem>();
        List<OrderItem> list = list(name);
        for (OrderItem c : list) {
            if (!c.getOrderItemId().equals(orderItemId)) {
                result.add(c);
                break;
            }
        }
        return result;
    }

    /**
     * 订单明细
     *
     * @return
     */
    public Object listOrderItemId() {
        List<OrderItem> orderItemList = orderItemDao.listAll();
        List<Long> result = new ArrayList<>();
        transferListId(orderItemList,result);
        return result;
    }

    private void transferListId(List<OrderItem> orderItemList,List<Long> result) {
        for (OrderItem item : orderItemList) {
            result.add(item.getOrderItemId());
        }
    }


    /**
     * 订单明细列表
     *
     * @param bo
     * @return
     */
    public Object list(SearchOrderItemBo bo) {
        PageVo<ListOrderItemVo> result = new PageVo<ListOrderItemVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(orderItemDao.list(bo));
        result.setTotal(orderItemDao.countList(bo));
        return result;
    }

    /**
     * 订单明细详情
     *
     * @param orderItemId
     * @return
     * @throws DzcxException
     */
    public Object detail(Long orderItemId) throws DzcxException {
        Object result = orderItemDao.detail(orderItemId);
        if (result == null) {
            throw new DzcxException(ExceptionCode.数据不存在);
        }
        return result;
    }

    /**
     * @param bo
     * @return
     * @throws DzcxException
     */
    public Object soldList(SearchOrderItemBo bo) throws DzcxException {
        List<Integer> statusList = Arrays.asList(OrderStatus.未支付.key(),OrderStatus.已提交支付.key(),
                OrderStatus.支付成功.key(),OrderStatus.用户支付中.key(),OrderStatus.已确认上车.key());
        PageVo<ListOrderItemVo> result = new PageVo<ListOrderItemVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());

        result.setItems(transfer(orderItemDao.soldList(bo,statusList)));
        result.setTotal(orderItemDao.countSoldList(bo,statusList));
        return result;
    }
    
    /**
     * ticket
     * @param bo
     * @return
     * @throws DzcxException
     */
    public Object ticketList(SearchOrderBo bo) throws DzcxException {
    	PageVo<ListOrderItemVo> result = new PageVo<ListOrderItemVo>();
    	result.setPageNum(bo.getPageNum());
    	result.setPageSize(bo.getPageSize());
    	
    	result.setItems(transfer(orderItemDao.ticketListPC(bo)));
    	result.setTotal(orderItemDao.countTicketListPC(bo));
    	return result;
    }

    private List<ListOrderItemVo> transfer(List<ListOrderItemVo> list) {
        list.forEach(item -> {
            item.setPhone(userDao.selectByPrimaryKey(item.getCreatorId()).getPhone());
            try {
            	DetailOrderVo order = orderRead.detail(item.getOrderId());
				item.setLineType(order.getLineType());
				item.setRideType(order.getRideType());
				item.setLineName(order.getLineName());
				item.setOrderNo(order.getOrderNo());
			} catch (Exception e) {
				e.printStackTrace();
			}
        });
        return list;
    }

	public PageVo<ListTicketVo> listTicket(ListTicketBo bo) {
		Date startDate = DateUtil.toDateOnly(new Date());
		bo.setStartDate(startDate);
    	PageVo<ListTicketVo> result = new PageVo<ListTicketVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        bo.setIsTicketAll(IsEnum.是.key());
        result.setItems(orderItemDao.listTicket(bo));
        result.setTotal(orderItemDao.countListTicket(bo));
		return result;
	}


}
